﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Input;
using Lib.Helper;
using MSIACCSL2.Command;
using MSIACCSL2.UserServices;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        #region Delegates

        public delegate void Completed(Object param);

        #endregion

        public static Completed OnLoadCompleted;
        private readonly UserServicesClient UserClient;
        private readonly IsolatedStorageSettings UserSettings;
        private UserDTO _User;
        private ObservableCollection<MenuDTO> _menus;
        private bool _IsBusy;

        public MainViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                UserClient = new UserServicesClient();
                NavigateCommand = new DelegateCommand(Navigate, CanNavigate);
                UserSettings = IsolatedStorageSettings.ApplicationSettings;
            }
        }


        public ICommand NavigateCommand { get; set; }
        public ICommand LoginCommand { get; set; }

        public ObservableCollection<MenuDTO> Menus
        {
            get { return _menus; }
            private set
            {
                if (Menus == value) return;
                _menus = value;
                FirePropertyChanged("Menus");
            }
        }

        public UserDTO User
        {
            get { return _User; }
            private set
            {
                if (User == value) return;
                _User = value;
                FirePropertyChanged("User");
            }
        }


        public bool  IsBusy
        {
            get { return _IsBusy; }
            set
            {
                if (_IsBusy == value) return;
                _IsBusy = value;
                FirePropertyChanged("IsBusy");
            }
        }

        public string SessionID
        {
            get { return UserSettings.FirstOrDefault().Key; }
        }

        public void Complete(Object param)
        {
            OnLoadCompleted(param);
        }

        public bool CanNavigate(Object param)
        {
            return true;
        }

        public bool CanLogin(Object param)
        {
            return true;
        }

        public void GetMenus(UserDTO user)
        {
            _User = user;
            var menus = new ObservableCollection<MenuDTO>();
            foreach (RoleDTO role in user.Roles)
            {
                foreach (MenuDTO menu in role.Menus.OrderBy(x=>x.Number))
                {
                    menus.Add(MapMenu(menu));
                }
            }

            BuildMenu(menus);
            User = user;

           
        }


        private void BuildMenu(ObservableCollection<MenuDTO> menus)
        {
            Menus = new ObservableCollection<MenuDTO>();
            foreach (MenuDTO parent in menus.Where(m => m.ParentMenu == null))
            {
                Menus.Add(parent);
                BuildSubMenu(parent, menus);
            }
            Complete(this);
        }


        private void BuildSubMenu(MenuDTO parent, ObservableCollection<MenuDTO> menus)
        {
            foreach (MenuDTO menu in menus.Where(m => m.ParentMenu != null && m.ParentMenu.MenuID == parent.MenuID))
            {
                if (parent.SubMenus == null) parent.SubMenus = new ObservableCollection<MenuDTO>();
                {
                    parent.SubMenus.Add(menu);
                    BuildSubMenu(menu, menus);
                }
            }
        }

        private MenuDTO MapMenu(MenuDTO menuDTO)
        {
            var menu = new MenuDTO();
            menu.Description = menuDTO.Description;
            menu.MenuID = menuDTO.MenuID;
            menu.MenuText = menuDTO.MenuText;
            menu.Number = menuDTO.Number;
            menu.URL = menuDTO.URL;
            if (menuDTO.ParentMenu != null)
                menu.ParentMenu = MapMenu(menuDTO.ParentMenu);
            return menu;
        }

        public void Navigate(Object param)
        {
        }
    }
}